<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8"/> 
<meta name="keywords" content="天地图"/> 
<title>天地图－地图API－范例－行政区划</title>
<style type="text/css">body,html{width:100%;height:100%;margin:0;font-family:"Microsoft YaHei"}#mapDiv{width:100%;height:400px}input,b,p{margin-left:5px;font-size:14px}</style>
<script type="text/javascript" src="//api.tianditu.gov.cn/api?v=4.0&tk=79d87cfedc91e46bcd9ac380d67f3035"></script>
<script> 
	var map; 
	var zoom = 12; 
	var administrative;
	var html = "<font style='font-weight:700'>详细信息如下：</font><br/>";
	function onLoad() 
	{ 
		//初始化地图对象 
	   	map=new T.Map("mapDiv"); 
	   	//设置显示地图的中心点和级别 
		map.centerAndZoom(new T.LngLat(116.40969,39.89945),zoom); 
		 
		//创建对象
		administrative = new T.AdministrativeDivision();
		var config = {
			needSubInfo: false,
			needAll: false,
			needPolygon: true,
			needPre: true,
			searchType: 1,
			searchWord: "合肥市"
		};
		administrative.search(config, searchResult);
	}
	
	function searchResult(result)
	{
		if(result.getStatus() == 100) 
		{
			var data = result.getData();
			showMsg(data);
			document.getElementById("administrativeMsg").innerHTML = html; 
		}
		else 
		{
			result.getMsg();
		}
	}
	
	function showMsg(data){
		for(var i = 0; i < data.length; i++){
			html += "<font style='font-size:12px'>行政区划名称："+data[i].name+"</font><br/>";
			html += "<font style='font-size:12px'>行政区划简称："+data[i].nameabbrevation+"</font><br/>";
			html += "<font style='font-size:12px'>行政区划英文名称："+data[i].english+"</font><br/>";
			html += "<font style='font-size:12px'>行政区划英文简称："+data[i].englishabbrevation+"</font><br/>";
			html += "<font style='font-size:12px'>行政区划类别(省市县)："+data[i].adminType+"</font><br/>";
			html += "<font style='font-size:12px'>行政区划码："+data[i].cityCode+"</font><br/>";
			html += "<font style='font-size:12px'>经度："+data[i].lnt+"</font><br/>";
			html += "<font style='font-size:12px'>纬度："+data[i].lat+"</font><br/>";
			html += "<font style='font-size:12px'>行政区划等级："+data[i].level+"</font><br/>";
			html += "<font style='font-size:12px'>四角点坐标："+data[i].bound+"</font><br/>";
			
			//解释上级行政区划
			if(data[i].parents){
				var upLevel = "";
				if(data[i].parents.country){
					upLevel += data[i].parents.country.name;
				}
				if(data[i].parents.province){
					upLevel += data[i].parents.province.name;
				}
				html += "<font style='font-size:12px'>上级行政区划："+upLevel+"</font><br/><br/>";
			}
			console.log(data[i].points)
			if(data[i].points){
				//绘制行政区划
				polygon(data[i].points);
                
			}
			
			//解释下级行政区划
			if(data[i].child){
				html += "<font style='font-weight:700'>下级行政区划：</font><br/>";
				showMsg(data[i].child);
				console.log(data[i].child.points);
				if(data[i].child.points){
					//绘制行政区划
					polygon(data[i].child.points);
				}
			}
		}
	}
	
	function polygon(points){
		var pointsArr = [];
        for (var i = 0; i < points.length; i++) {
            var regionLngLats = [];
            var regionArr = points[i].region.split(",");
            for (var m = 0; m < regionArr.length; m++) {
                var lnglatArr = regionArr[m].split(" ");
                var lnglat = new T.LngLat(lnglatArr[0], lnglatArr[1]);
                regionLngLats.push(lnglat);
                pointsArr.push(lnglat);
            }
          	//创建面对象
	        var polygon = new T.Polygon(regionLngLats,{color: "blue", weight: 3, opacity: 1, fillColor: "#FFFFFF", fillOpacity: 0.3});
	      	//向地图上添加行政区划面
	        map.addOverLay(polygon);
            console.log(456456456564)
        }
        //显示最佳比例尺
        map.setViewport(pointsArr);
	}
</script> 
</head> 
<body onLoad="onLoad()"> 
	<div id="mapDiv"></div> 
	<p>本示例演示如何使用行政区划接口获得详细信息。</p>
	<div id="administrativeMsg" style="font-size:14px"></div>
</body>
</html>